/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateLongClickListener;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;

import org.threeten.bp.format.DateTimeFormatter;

/**
 * @since 2021-07-12
 */
public class BasicAbility extends BaseAbility implements OnDateSelectedListener,
    OnDateLongClickListener, OnMonthChangedListener {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");
    private Text textView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        MaterialCalendarView widget = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_ACCENT);
        widget.setOnDateChangedListener(this);
        widget.setOnDateLongClickListener(this);
        widget.setOnMonthChangedListener(this);
        textView = (Text) findComponentById(ResourceTable.Id_textView);
        //Setup initial text
        textView.setText("No Selection");
        toolbarTitle.setText("Basic Example");
    }

    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_basic;
    }

    @Override
    public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
        textView.setText(selected ? FORMATTER.format(date.getDate()) : "No Selection");
    }

    @Override
    public void onDateLongClick(MaterialCalendarView widget, CalendarDay date) {
        final String text = String.format("%s is available", FORMATTER.format(date.getDate()));
        new ToastDialog(this).setText(text).show();
    }

    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
        //noinspection ConstantConditions
        toolbarTitle.setText(FORMATTER.format(date.getDate()));
    }
}
